import Foundation

public class ListNode {
    public var val: Int
    public var next: ListNode?
    public init() { self.val = 0; self.next = nil; }
    public init(_ val: Int) { self.val = val; self.next = nil; }
    public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
}

func reverseListNode(head: ListNode?) -> ListNode? {
    var curr = head
    var pre: ListNode? = nil
    while (curr != nil) {
        let next = curr?.next
        curr?.next = pre
        pre = curr
        curr = next
    }
    return pre
}
